package course

import (
	"encoding/json"

	"net/http"

	"awfan.cn/utils"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
)

func create(format string, w http.ResponseWriter, body []byte, db *mgo.Database) {
	data := COURSE_XML{}

	err := json.Unmarshal(body, &data)

	if err != nil {
		utils.WriteError(format, 404, err.Error(), w)
		return
	}

	data.Id = bson.NewObjectId()
	data.Created_time = bson.Now()

	c := db.C(utils.PkgName(COURSE_XML{}))

	err = c.Insert(&data)

	if err != nil {
		utils.WriteError(format, 404, err.Error(), w)
		return
	}

	utils.WriteData(format, make(map[string]interface{}), w)
}
